var Tower = cc.MenuItemImage.extend({
    _series : 0,
    _canFire : true,
    _point : cc.p(0,0),
    // for init bullet
    _power : 0,
    _speed : 0,
    _bulletType : -1,
    // init from file data
    _fireTime : 0,
    _fireRange : 0,
    // some buffer
    _bullets : null, // just for a buffer to get bullet and revive it
    ////////////////
    _price : 0,
    ///////////////////////////////////////////////////////////
    ctor : function( filename,towerData ){//series,power,speed,fireRange,fireTime,bulletRes
        this._super(filename);
        this._bullets = new Array();
        // for bullet
        this._power = towerData.pw[0];
        this._speed = towerData.sp[0];
        this._bulletType = towerData.type;
        // properties
        this._fireRange = towerData.fr[0];
        this._fireTime = towerData.ft[0];
    },
    onEnter:function () {
        this._super();
    },
    onExit : function(){
        var length = this._bullets.length;
        for( var i=0;i<length;i++){
            var bullet = this._bullets[i];
            bullet.setOwner(null); // bullet.removeFromParent(true);
            cc.log( "********set bullet owner null: "+bullet.getType() );
        }// just set it's owner null,do not remove it from main layer for update bullet when u remove the tower , and remove it in main loop//*/
        this._bullets.splice(0,length); // just clear buffer
        this._super();
    },
    addBullet : function(bullet){
        // add to this bullets buffer just for reuse it
        this._bullets.push( bullet );
        // add to main layer just for update it right when u remove the tower
        var mainLayer = GetMainLayer();
        mainLayer.addBullet( bullet );
    },
    getBulletFromBuffer : function(){
        for (var i=0;i<this._bullets.length;++i){
            var bullet = this._bullets[i];
            if( !bullet.getLife() ){
                cc.log("get bullet from buffer");
                return bullet;
            }
        }
        return null;
    },
    ///////////////////////////////////////
    addPrice : function(menoy){
        this._price+=menoy;
    },
    getPrice : function(){
        return this._price;
    },
    ///////////////////////////////////////////////////////////////////
    setPoint : function(x,y){
        this._point.x=x,this._point.y=y;
    },
    setPoint : function(point){
        this._point=point;
    },
    getBullets : function(){
        return this._bullets;
    },
    getPoint : function(){
        return this._point;
    },
    getSeries: function(){
        return  this._series;
    },
    setSeries: function(series){
        this._series=series;
    },
    /////////////////////////////////////////////////
    updateSeries : function(){
        var data=this.getUserData();
        var series=this._series;
        this.updateSkin(data,series);
        this.updatePower(data,series);
        this.updateSpeed(data,series);
        this.updateFireTime(data,series);
        this.updateFireRange(data,series);
        this._series=++series;
        cc.log("series: "+series);
    },

    updatePower :  function(data,series){
        if(series<data.pw.length){
            this._power = data.pw[series];
            cc.log("update power: "+this._power);
        }else{
            cc.log("top power");
        }
    },
    updateSpeed :  function(data,series){
        if(series<data.sp.length){
            this._speed = data.sp[series];
            cc.log("update speed: "+this._speed);
        }else{
            cc.log("top speed");
        }
    },
    updateFireTime :  function(data,series){
        if(series<data.ft.length){
            this._fireTime = data.ft[series];
            cc.log("update fire time space: "+this._fireTime);
        }else{
            cc.log("top fire time space");
        }
    },
    updateFireRange :  function(data,series){
        if(series<data.fr.length){
            this._fireRange = data.fr[series];
            cc.log("update fire range: "+this._fireRange);
        }else{
            cc.log("top fire range");
        }
    },
    updateSkin : function(data,series){
        if(series<data.res.length){
            var name = data.res[series];
            var filename = MainResource( name );
            var frame = new cc.SpriteFrame( filename,cc.rect(0,0,70,70) );
            this.setNormalSpriteFrame(frame);
            this.setSelectedSpriteFrame(frame);
            this.setDisabledSpriteFrame(frame);
            cc.log("skin res: "+name);
        }else{
            cc.log("end skin");
        }
    }
});


